import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useSessionStore = defineStore('session', () => {
    const session = ref({
        id: 0,
        title: '',
        content: '',
        mid: '',
        createtime: '',
        updatetime: '',
        modelAvatar: '',
        modelName: '',
    })

    function setSession(newSession) {
        if (!newSession) return
        // 改为逐个属性更新而不是整体替换
        Object.keys(newSession).forEach(key => {
            if (key in session.value) {
                session.value[key] = newSession[key]
            }
        })
    }

    function clearSession() {
        session.value = {
            id: 0,
            title: '',
            content: '',
            mid: '',
            createtime: '',
            updatetime: '',
            modelAvatar: '',
            modelName: '',
        }
    }

    return { session, setSession, clearSession }

}, {
    persist: true
})